package com.weilaiwd.admin.aop;

import java.util.Arrays;
import java.util.List;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;

public class LoginInterceptor implements Interceptor {
	
	private List<String> excepts;
	
	public LoginInterceptor(String[] excepts) {
		this.excepts = Arrays.asList(excepts);
	}

	@Override
	public void intercept(ActionInvocation ai) {
		Controller c = ai.getController();
		
		String requestUri = c.getRequest().getRequestURI().replaceFirst(c.getRequest().getContextPath(), "");
		if(excepts.contains(requestUri))
			ai.invoke();
		else {
			Object obj = c.getSessionAttr("admin_login_user");
			if(obj==null)
				c.redirect("/login");
			else
				ai.invoke();
		}
	}

}
